import Toast from './toast';

let currentToast;

export default {
  install(Vue, options) {
    Vue.prototype.$toast = function (message, toastOptions) {
      if(currentToast) currentToast.close();
      currentToast = createToast({Vue, message, propsData: toastOptions, onClose:()=>{currentToast = null;}});
    }
  }
}

function createToast({Vue, message, propsData, onClose}) {
  var Constructor = Vue.extend(Toast);
  let toast = new Constructor({propsData});
  toast.$slots.default = [message]; //必须数组 必须放mount前
  toast.$mount(); //不mount toast所有的生命周期的钩子都不会执行
  toast.$on('close', onClose);
  document.body.appendChild(toast.$el);
  return toast;
}
